package com.mhg.thread;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo {
    public static void main(String[] args) {
        Thread t0 = new Thread(r,"喜洋洋");// 参数二 是重定义线程名
        Thread t1 = new Thread(r,"灰太狼");
        Thread t2 = new Thread(r,"懒羊羊");

        t0.start();
        t1.start();
        t2.start();
    }

    static int count = 200;
    static ReentrantLock lock = new ReentrantLock();
    static Runnable r = new Runnable(){

        @Override
        public void run() {
            while(count>0){
                //加锁
                lock.lock();
                if(count <=0){
                    return;
                }
                count--;
                System.out.println(Thread.currentThread().getName()+",,,,"+count);
                //解锁
                lock.unlock();
                //注意 lock()方法 和 unlock() 都是成对出现的
            }
        }
    };
}
